//
//  LoginViewController.m
//  DeviceManage
//
//  Created by zxl on 2020/10/19.
//  Copyright © 2020年 sjl. All rights reserved.
//

#import "LoginViewController.h"
#import "User.h"
@interface LoginViewController ()
@end
@implementation LoginViewController
- (void) viewDidLoad {
    [super viewDidLoad];
    // 检查是否已经登录，如果已经登录，则直接进入主界面
    if ([[User shareInstance] checkLogin] ) {
        // 已登录
        [self showMainViewController];
        return;
    } 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
// 回调函数 loginCallback
- (void)loginCallback:(User*)loginUser {
} 
-(void)showMainViewController{
    //通过单例 UIApplication 对象获得 app 的委托类 AppDelegate 对象
    AppDelegate *appDelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    //通过委托类对象设置新的主界面，即从当前的登录界面跳转到主界面
    appDelegate.window.rootViewController=[[MainController alloc]init];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)loginValidate:(id)sender {
    //显示转圈圈控件
    [SVProgressHUD show];
    // 初始化参数
    NSDictionary *params=@{@"username":self.usernameTF.text,
                           @"password":self.passwordTF.text};
    //执行网络访问请求
    [[NetworkTool shareInstance]requireMethodType:POSTType
                                        URLString:@"DeviceManage/loginValidate"
                                       Parameters:params
                                          Success:^(NSDictionary *respondDictionary) {
                                              // 如果 respondDictionary 中为空,直接说明登陆错误，直接返回
                                              if([[respondDictionary[@"result"][0] allKeys] count]==0){
                                                  [SVProgressHUD dismiss];
                                                  [SVProgressHUD showErrorWithStatus:@"web 接口服务连接失败。1.请确保主机 ip 地址是否正确，然后打开 tomcat 服务器。2.确保用户名和密码正确"];
                                                   return;
                                                   }
                                                   // 如果 respondDictionary 中不为空,说明登陆正确
                                                   [SVProgressHUD showInfoWithStatus:@"恭喜你，登陆成功"];
                                                   // 将登陆成功的用户保存到手机上
                                                   [[User shareInstance]setValuesForKeysWithDictionary:
                                                    respondDictionary[@"result"][0]];
                                                   [SVProgressHUD dismiss];
                                                   //打开主页面控制器
                                                   [self showMainViewController];
                                                   }
                                                                             Failure:^(NSError *error) {
                                                                                 [SVProgressHUD dismiss];
                                                                                 [SVProgressHUD showErrorWithStatus:@"web 接口服务连接失败。1.请确保主机 ip 地址是否正确，然后打开 tomcat 服务器。2.确保用户名和密码正确"];
                                                                                  }];
}
@end
